﻿using System;
using UnityEngine;

namespace GxFramework
{
    public class DefaultSettingHandler : ISettingHandler
    {
        public bool Load()
        {
            return true;
        }
        public bool Save()
        {
            PlayerPrefs.Save();
            return true;
        }
        public void Remove(string key)
        {
            PlayerPrefs.DeleteKey(key);
        }
        public void RemoveAll()
        {
            PlayerPrefs.DeleteAll();
        }
        public bool HasKey(string key)
        {
            return PlayerPrefs.HasKey(key);
        }
        public bool GetBool(string key, bool defaultValue)
        {
            return PlayerPrefs.GetInt(key,defaultValue?1:0) != 0;
        }
        public int GetInt(string key, int defaultValue)
        {
            return PlayerPrefs.GetInt(key,defaultValue);
        }
        public float GetFloat(string key, float defaultValue)
        {
            return PlayerPrefs.GetFloat(key, defaultValue);
        }
        public string GetString(string key, string defaultValue)
        {
            return PlayerPrefs.GetString(key,defaultValue);
        }
        public object GetObject(string key,Type type, object defaultValue)
        {
            var text = PlayerPrefs.GetString(key, string.Empty);
            if (string.IsNullOrEmpty(text))
                return defaultValue;
            return JsonUtility.FromJson(text,type);
        }

        public void SetBool(string key, bool value)
        {
            PlayerPrefs.SetInt(key,value ? 1:0);
        }
        public void SetInt(string key, int value)
        {
            PlayerPrefs.SetInt(key, value);
        }
        public void SetFloat(string key, float value)
        {
            PlayerPrefs.SetFloat(key, value);
        }
        public void SetString(string key, string value)
        {
            PlayerPrefs.SetString(key, value);
        }
        public void SetObject(string key, object value)
        {
            PlayerPrefs.SetString(key, JsonUtility.ToJson(value));
        }
    }
}
